package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.domain.basic.MobileMessage;
import cc.yugu.yhdw.common.domain.basic.SmsConfigInfo;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.MobileMessageMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 短信发送业务
 *
 * @Date:Created in 2018/7/3 17:12
 * @Author:
 * @Modified By: zjn 2018/7/3 17:12
 */
@Service
public class MobileMessageService extends AbstractService {
    @Autowired
    MobileMessageMapper mobileMessageMapper;
    /**
    * 查询
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.basic.MobileMessage
    * @Author By: zjn 2018/7/3  17:12
    */
    public MobileMessage find(long id) {
        // 根据主键id查询表 bas_mobile_message
        MobileMessage record = mobileMessageMapper.find(id);
        // 如果查询的数据不为null且 发送人不为null
        if(record != null && record.getSenderId() != null) {
            SmsConfigInfo smsConfigInfo = findSmsConfigInfo(record.getSenderId());
            if(smsConfigInfo != null) {
                // 设置短信 名称
                record.setSenderName(smsConfigInfo.getConfigName());
            }
        }
        return record;
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/3  17:21
    */ 
    public Page findPage(MobileMessage search) {
        List<MobileMessage> list = null;
        Page page = search.buildPage();
        page.setTotalItems(mobileMessageMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(list = mobileMessageMapper.findPageResult(search));

        for(MobileMessage e : list) {
            if(e != null && e.getSenderId() != null) {
                SmsConfigInfo smsConfigInfo = findSmsConfigInfo(e.getSenderId());
                if(smsConfigInfo != null) {
                    e.setSenderName(smsConfigInfo.getConfigName());
                }
            }
        }
        return page;
    }
    /**
    * 插入
    * 
    * @Param: [mobileMessage]
    * @Return: int
    * @Author By: zjn 2018/7/3  17:21
    */ 
    public int insert(MobileMessage mobileMessage) {
        if(StringUtils.isEmpty(mobileMessage.getMobile())) {
            throw new IllegalArgumentException();
        }

        return mobileMessageMapper.insert(mobileMessage);
    }
    /**
    * 根据主键更新状态信息
    * 
    * @Param: [id, status]
    * @Return: int
    * @Author By: zjn 2018/7/3  17:21
    */ 
    public int updateStatus(long id, int status) {
        return mobileMessageMapper.updateStatus(id, status);
    }
}
